<template>
	<view class="cu-form-group">
		<!-- <view class="cu-btn bg-green w100 round3 ripple" @click="submit">保存</view> -->
		<view class="cu-btn lg tac line-green pos-r round w100" @tap="submit">
			{{label}}
			<ripple></ripple>
		</view>
	</view>
</template>

<script>
	import api from '../../assets/js/api.js'
	export default {
		name: "ssubmit",
		props: {
			value: '',
			model: '',
			label: {
				type: String,
				default: "保存"
			},
			placeholder: '',
			callback: '',
			url: ''
		},
		data() {
			return {
				valueInside: '',
				cb: function() {}
			}
		},
		methods: {
			submit() {
				if (this.value === '') {
					return false
				}
				let url = '/api/' + this.model

				if (this.value.id != undefined) {

					if (this.$com.isbool(this.url)) {
						url = this.url
					} else {
						url = url + '/' + this.value.id
					}
					api.patch(url, this.value, (res) => {
						this.$com.showToast(res.i)
						this.cb(res)
					})
				} else {
					if (this.$com.isbool(this.url)) {
						url = this.url
					}
					api.post(url, this.value, (res) => {
						this.$com.showToast(res.i)
						this.cb(res)
					})
				}
			}
		},
		mounted() {
			if (!this.$com.isbool(this.callback)) {
				this.cb = (res) => {
					if (res.s == 1) {
						this.$com.showToast(res.i)
						setTimeout(() => {
							this.$win.goback()
						}, 500)
					}else{
						this.$com.showToast(res.i)
					}
				}
				return
			} else {
				this.cb = this.callback
			}
		},
		watch: {
			value(curVal, oldVal) {
				this.valueInside = curVal
			},
			valueInside(curVal, oldVal) {
				this.$emit('input', curVal)
			}
		}
	}
</script>

<style>
	.cu-form-group .title {
		min-width: calc(4em + 15px);
	}
</style>
